home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / t_os / whisper / source / maksnd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-19  |  3.8 KB  |  172 lines

  1. /*-------------------------------------------------------------
  2. *  PCM 録音 for WHISPER
  3. * Copyright (c) 1990 Nanno-NETWORK Allright Reserved
  4. * Programmed & Designed by Hirokazu Fujinaka
  5. **** Edition History ***
  6. *--- date --- --- auther --- --- contents ---
  7. *1990 Oct. 13 H.Fujinaka     作成開始:初期バ-ジョン
  8. *--------------------------------------------------------------*/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <snd.h>
  13. #include <string.h>
  14.  
  15. #define  ERROR (-1)
  16. /*-----------------------------------------------------
  17. *ファイル外部でサウンド関連の初期化がされていれば
  18. * INITEDを定義すること。
  19. *----------------------------------------------------*/
  20. #define  INITED
  21.  
  22.  
  23. #define  PCMCH 71
  24. #define  MAXCH 1
  25.  
  26. #define LINE 0
  27. #define CD   1
  28. #define MIC  2
  29. #define MODEM 3
  30.  
  31. #define SRC_LNR 4
  32. #define SRC_LNL 8
  33. #define SRC_CDR 16
  34. #define SRC_CDL 32
  35. #define SRC_MIC 64
  36. #define SRC_MOD 128
  37.  
  38. #define VOL_LIN 0
  39. #define VOL_CD  1
  40. #define VOL_MIC 2
  41. #define VOL_MOD 3
  42. /*----------------------------------------------------
  43. * 再生時のためのファイルヘッダ-構造体
  44. *---------------------------------------------------*/
  45. struct header { char      name[8];
  46.                 long      id;
  47.                 long      length;
  48.                 long      loop_pos;
  49.                 long      loop_len;
  50.                 short int freq,
  51.                           adjust;
  52.                 char      original_tone;
  53.                 char      flag;
  54.                 short int reserved;
  55.               } head;
  56.  
  57. static
  58. int id=0,                   /* sound ID       */
  59.     src=MIC,                /* input device   */
  60.     vol=127,                /* volume         */
  61.     freq=20*1000,           /* sampling freq. */  
  62.     trig=0;                 /* sampling level */
  63.  
  64.  
  65. #ifdef INITED
  66. static char *snd_w;
  67. #endif
  68.  
  69. static char *buff;
  70.  
  71.  
  72.  
  73. static void write_head(FILE *fd, char *name)
  74. {
  75.   strncpy(head.name, name, 8);
  76.   srand((unsigned int) 16387);
  77.   head.id=(id == 0 ?((long)rand()) : id );
  78.   head.length=       (long)     sizeof(buff);
  79.   head.loop_pos=     (long)     (sizeof(buff)-1);
  80.   head.loop_len=     (long)     0;
  81.   head.freq=         (short int)freq;
  82.   head.adjust=       (short int)0;
  83.   head.original_tone=(char)     60;
  84.   head.flag=         (char)     0;
  85.   head.reserved=     (short int)0;
  86.   fwrite(&head,sizeof(head),1,fd);
  87.   
  88. }
  89.  
  90. static int write_file(char *name)
  91. { FILE *fd;
  92.  
  93.   if ( ( fd=fopen(name,"wb") ) == NULL )
  94.   return (ERROR);
  95.   
  96.   write_head(fd, name);
  97.   fwrite(buff,sizeof(buff),1,fd);
  98.   fclose(fd);
  99.   return( 0 );
  100. }
  101.  
  102.  
  103. static void setup_pcm()
  104. {
  105.   SND_pcm_mode_set(MAXCH);
  106.   SND_elevol_init();
  107.   SND_elevol_mute(0);
  108.   SND_elevol_set(src,vol,vol);
  109. }
  110.  
  111.  
  112. /*-----------------------------------------------------------------
  113. *
  114. *     SND_make(char *file);
  115. *
  116. *
  117. * ファイル名は、こちらから指定して戻り値としてエラ-の有無を
  118. *
  119. * 通知していただけたら幸いです。
  120. *
  121. * 知命的エラ-(malloc,fopenなど)で (-1)
  122. * 録音中断なら              1
  123. * 正常録音なら              0
  124. *
  125. * で、オ-トトリガなら最高?かな・・・(こいつは、あくまで希望)
  126. *
  127. ---------------------------------------------------------------------*/
  128. static int init()
  129. {
  130.   if ( (buff=(char *)malloc(65536)) == NULL ) return (ERROR);
  131. #ifdef INITED
  132.   if ( (snd_w=(char *)malloc(16384)) == NULL) return (ERROR);
  133.   SND_init(snd_w);
  134. #endif
  135.   return ( 0 );
  136. }
  137.  
  138.  
  139.  
  140. static int rec(char *filename)
  141. { int ret;
  142.  
  143.   setup_pcm();
  144.   SND_pcm_rec(freq,buff,sizeof(buff),trig);
  145.   ret=write_file(filename);
  146.   return( ret );
  147.  
  148. }
  149.  
  150. static void term()
  151. {
  152. #ifdef INITED
  153.   SND_end();
  154.   free(snd_w);
  155. #endif
  156.   SND_elevol_mute(0xB3);
  157.   free(buff);
  158. }
  159.  
  160.  
  161. int SND_make( char *filename )
  162. { int result;
  163.  
  164.   result=init();
  165.   if( result == 0 )
  166.   { 
  167.     result=rec( filename );
  168.     term(); 
  169.   }
  170.   return( result );
  171. }
  172.